package com.shenchuangit.webprintclient.server;

import com.shenchuangit.webprintclient.cycle.AbstractPrintJobHandler;
import com.shenchuangit.webprintclient.cycle.AbstractPrintJobPreHandler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class WebPrintRequestServer {

    public void init(AbstractPrintJobPreHandler printJobPreHandler, AbstractPrintJobHandler printJobHandler) {
        Server server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        // Or ServletContextHandler.NO_SESSIONS
        context.setContextPath("/");
        server.setHandler(context);

        // http://localhost:8080/printJob
        PrintJobServlet printJobServlet = new PrintJobServlet();
        printJobServlet.setPrintJobPreHandler(printJobPreHandler);
        printJobServlet.setPrintJobHandler(printJobHandler);
        context.addServlet(new ServletHolder(printJobServlet), "/printJob");

        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            server.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
